﻿using Common.Core.Helpers.Json;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;

namespace Common.Core.Extensions
{
    /// <summary>
    /// JSON扩展，序列化、反序列化扩展类。 
    /// </summary>
    public static class JsonExtentions
    {

        /// <summary>
        /// 对象序列化成JSON字符串。
        /// </summary>
        /// <param name="obj">序列化对象</param>
        /// <param name="writeIndented">是否格式化json字符串</param>
        /// <returns></returns>
        public static string ToJson(this object obj, bool writeIndented = true)
        {
            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                WriteIndented = writeIndented,                          //格式化json字符串
                AllowTrailingCommas = true,                             //可以结尾有逗号
                IgnoreReadOnlyProperties = true,                        //忽略只读属性
                PropertyNameCaseInsensitive = true,                     //忽略大小写
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
                //DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
            };
            options.Converters.Add(new DateTimeJsonConverter());
            options.Converters.Add(new DateTimeNullableConverter());
            options.Converters.Add(new BooleanJsonConverter());
            //设置数字
            options.Converters.Add(new IntJsonConverter());

            return JsonSerializer.Serialize(obj, options);
        }

        /// <summary>
        /// JSON字符串序列化成对象。
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="json">JSON字符串</param>
        /// <returns></returns>
        public static T JsonToObject<T>(this string json)
        {
            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                WriteIndented = true,                                   //格式化json字符串
                AllowTrailingCommas = true,                             //可以结尾有逗号
                IgnoreReadOnlyProperties = true,                        //忽略只读属性
                PropertyNameCaseInsensitive = true,                     //忽略大小写
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
                //DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
            };
            options.Converters.Add(new DateTimeJsonConverter());
            options.Converters.Add(new DateTimeNullableConverter());
            options.Converters.Add(new BooleanJsonConverter());
            //设置数字
            options.Converters.Add(new IntJsonConverter());

            return json == null ? default(T) : JsonSerializer.Deserialize<T>(json, options);
        }

        /// <summary>
        /// JSON字符串序列化成集合。
        /// </summary>
        /// <typeparam name="T">集合类型</typeparam>
        /// <param name="json">JSON字符串</param>
        /// <returns></returns>
        public static List<T> JsonToList<T>(this string json)
        {
            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                WriteIndented = true,                                   //格式化json字符串
                AllowTrailingCommas = true,                             //可以结尾有逗号
                IgnoreReadOnlyProperties = true,                        //忽略只读属性
                PropertyNameCaseInsensitive = true,                     //忽略大小写
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
                //DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
            };
            options.Converters.Add(new DateTimeJsonConverter());
            options.Converters.Add(new DateTimeNullableConverter());
            options.Converters.Add(new BooleanJsonConverter());
            //设置数字
            options.Converters.Add(new IntJsonConverter());

            return json == null ? null : JsonSerializer.Deserialize<List<T>>(json, options);
        }
    }
}
